package de.bht.cge.flightsim.game;

import java.io.File;

import de.bht.cge.flightsim.util.Helper;
import de.bht.cge.flightsim.util.Settings;
import de.bht.jvr.collada14.loader.ColladaLoader;
import de.bht.jvr.core.Finder;
import de.bht.jvr.core.Geometry;
import de.bht.jvr.core.ShaderMaterial;
import de.bht.jvr.core.ShaderProgram;
import de.bht.jvr.core.ShapeNode;
import de.bht.jvr.core.Texture2D;
import de.bht.jvr.core.Transform;
import de.bht.jvr.math.Vector3;


public class Skybox extends Base
{
	
	private static String shaderContext = "SKYPASS";	
	private static String textureUniform = "myTexture";
	
	public Skybox(Vector3 position)
	{
		this.setScale(Settings.SkyboxScale);
		this.setName("Skybox");
		this.setPosition(position);
		
		try
		{
			this.Build();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	@Override
	public void Build() throws Exception
	{
		super.Build();
		
		Geometry geo = Finder.findGeometry(ColladaLoader.load(new File("meshes/plane.dae")), null);
		
		float posDist = this.getScale() / 2.0f;
		posDist = posDist < 0.1f ? 0.1f : posDist;
		posDist -= 0.01f;
		float negDist = -posDist;
		ShaderProgram prog = Helper.LoadShader(new File("shaders/skybox/skybox.vs"), new File("shaders/skybox/skybox.fs"));
		
		ShaderMaterial backMaterial = Helper.CreateShaderMaterial(new Texture2D(new File(Settings.backTextureFile)), prog, "Skybox", textureUniform, shaderContext);
		ShaderMaterial frontMaterial = Helper.CreateShaderMaterial(new Texture2D(new File(Settings.frontTextureFile)), prog, "Skybox", textureUniform, shaderContext);
		ShaderMaterial leftMaterial = Helper.CreateShaderMaterial(new Texture2D(new File(Settings.leftTextureFile)), prog, "Skybox", textureUniform, shaderContext);
		ShaderMaterial rightMaterial = Helper.CreateShaderMaterial(new Texture2D(new File(Settings.rightTextureFile)), prog, "Skybox", textureUniform, shaderContext);
		ShaderMaterial topMaterial = Helper.CreateShaderMaterial(new Texture2D(new File(Settings.topTextureFile)), prog, "Skybox", textureUniform, shaderContext);
		ShaderMaterial bottomMaterial = Helper.CreateShaderMaterial(new Texture2D(new File(Settings.bottomTextureFile)), prog, "Skybox", textureUniform, shaderContext);
		
		ShapeNode backShape = new ShapeNode("Skybox Back Face",geo, backMaterial);
		backShape.setTransform(Transform.translate(0, 0, negDist).mul(Transform.scale(this.getScale())));

		ShapeNode leftShape = new ShapeNode( "Skybox Left Face",geo, leftMaterial); 
		leftShape.setTransform(Transform.translate(negDist, 0, 0).mul(Transform.rotateYDeg(90)).mul(Transform.scale(this.getScale())));
		
		ShapeNode rightShape = new ShapeNode( "Skybox Right Face",geo, rightMaterial); 
		rightShape.setTransform(Transform.translate(posDist, 0, 0).mul(Transform.rotateYDeg(-90)).mul(Transform.scale(this.getScale())));
		
		ShapeNode frontShape = new ShapeNode("Skybox Front Face",geo, frontMaterial); 
		frontShape.setTransform(Transform.translate(0, 0, posDist).mul(Transform.rotateYDeg(180)).mul(Transform.scale(this.getScale())));
		
		ShapeNode topShape = new ShapeNode("Skybox Top Face",geo, topMaterial);
		topShape.setTransform(Transform.translate(0, posDist, 0).mul(Transform.rotateXDeg(90)).mul(Transform.scale(this.getScale())));
		
		ShapeNode bottomShape = new ShapeNode("Skybox Bottom Face",geo, bottomMaterial);
		bottomShape.setTransform(Transform.translate(0, negDist, 0).mul(Transform.rotateXDeg(-90)).mul(Transform.scale(this.getScale())));
		
		this.addChildNodes(backShape,leftShape,rightShape,frontShape,topShape,bottomShape);
		this.setTransform(Transform.translate(this.getPosition()));
	}
	
	
}
